<?php
class Core_Data_Mappers_Resourceprivilege extends Core_Data_Mappers_Abstract {
	protected $_modelclass="Core_Data_Models_Resourceprivilege";
	protected $_businessclass="Core_Authentication_Business_Resourceprivilege";
	protected $_map=array("PrivilegeId"=>"privilegeid","ResourceId"=>"resourceid","RuleId"=>"ruleid","Allow"=>"isallow","Created"=>"created","Updated"=>"updated");
	
	public function find($privilege,$resource,$rule) {
		$row = $this->getDbTable()->fetchRow(array("privilegeid = ?"=>$privilege,"resourceid=?"=>$resource,"ruleid=?"=>$rule));
		if(isset($row) && count($row)>0) {
			return $this->map($row->current);
		}
		return null;
	}
	
	public function exists($object) {
		$o = $this->find($object->PrivilegeId,$object->ResourceId,$object->RuleId);
		if(isset($o) && $o instanceof Core_Authentication_Business_Resourceprivilege) {
			return true;
		}
		else {
			return false;
		}
		
	}
	public function add($object) {
		if(!$this->exists($object)) {
			$data = $this->reversemap($object);
			$this->getDbTable()->insert($data);		
		}
	}
	public function edit($object) {
		if($this->exists($object)) {
			$data = $this->reversemap($object);
			unset($data['privilegeid']);
			unset($data['resourceid']);
			unset($data['ruleid']);
			$this->getDbTable()->update($data, array("privilegeid = ?"=>$privilege,"resourceid=?"=>$resource,"ruleid=?"=>$rule));
		}
		
	}
	public function fetchAll($where=null,$count=10,$start=0) {
		$rows = $this->getDbTable()->fetchAll($where,null,$count,$start);
		$collection = array();
		if(isset($rows) && count($rows)>0) {
			foreach ($rows as $item) {
				$collection[] = $this->map($item);
			}
		}
		return $collection;
	}
}

?>